﻿using System;
using System.Data;
using System.Text;
using ZHDJ.Data.Models.Center;
using ZHDJ.Data.Models.Common;
using ZHDJ.Data.Models.JoinParty;
using ZHDJ.WebPage.Master;

namespace ZHDJ.Web
{
    public partial class MorePage : UseMorePageMasterPage
    {
        private string BranchID
        {
            get
            {
                return this.Request["BranchID"] ?? "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                hfType.Value = this.Request["type"] ?? "";
                hfSearchText.Value = SearchText;
                BindData();
            }
        }

        public void LoadData()
        {
            if (this.Module == "PartyFactor")
            {
                DataSet ds = PartyArticle.GetPartyArticleGridData(0, 1, this.Module, this.Category, "", "");
                this.ltContent.Visible = true;
                this.ltContent.Text = ds.Tables[0].Rows[0]["Content"].ToString();

                listControl.Style["display"] = "none";
                contentControl.Style["display"] = "block";
            }
            else
            {
                listControl.Style["display"] = "block";
                contentControl.Style["display"] = "none";
            }
        }

        private void BindData()
        {
            string module = string.IsNullOrEmpty(hfModule.Value) ? this.Module : hfModule.Value;
            bool isTreasureBox = module == "TreasureBox";
            bool isDownloadLibrary = module == "PartySchoolOnline" && Category == "DownloadLibrary";
            bool isVolunteerActivitie = module == "VolunteerActivitie" && this.Category == "Call";
            bool isVolunteerQuotation = module == "VolunteerActivitie" && this.Category == "Quotation";
            bool isJoinPartyPublicity = module == "JoinPartyPublicity" && this.Category.IndexOf("publicity") > -1;
            bool isRelationShipPublicity = module == "RelationShippublicity" && this.Category == "relationpub";
            bool isPartyPayment = module == "PartyPayment" && this.Category == "Payment";
            bool isWebsiteSearch = module == "WebsiteSearch";
            bool isDownloadCadreWork = module == "CadreWork" && this.Category == "Download";//干部工作-常见下载
            bool isMassLineSuperiorsSpirit = module == "MassLine" && this.Category == "SuperiorsSpirit";//上级精神

            StringBuilder sbHtml = new StringBuilder();
            sbHtml.Length = 0;
            DataTable dt = new DataTable();
            DataTable dtCount = new DataTable();
            int sumCount = 0;
            int startIndex = (AspNetPager1.CurrentPageIndex - 1) * AspNetPager1.PageSize;

            if (isWebsiteSearch)
            {
                var aa = hfSearchText.Value;
                dtCount = null;
                dtCount = PartyArticle.GetWebsiteSearchResult(-1, -1, this.SearchText).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyArticle.GetWebsiteSearchResult(AspNetPager1.PageSize, startIndex, this.SearchText).Tables[0];
                sbHtml.Append(ListHtml(dt));
            }
            else if (isJoinPartyPublicity)
            {
                if (this.hfType.Value == "")
                {
                    dtCount = null;
                    dtCount = partyjoinpublicity.GetListDataByType(-1, -1, "JoinStep4").Tables[0];
                    BindPageControl(dtCount, sumCount);

                    dt = partyjoinpublicity.GetListDataByType(startIndex, AspNetPager1.PageSize, "JoinStep4").Tables[0];
                    sbHtml.Append(ListHtml(dt));
                }
                else
                {
                    dtCount = null;
                    dtCount = partyjoinpublicity.GetListDataByType(-1, -1, this.hfType.Value).Tables[0];
                    BindPageControl(dtCount, sumCount);

                    //根据分类获取
                    dt = partyjoinpublicity.GetListDataByType(startIndex, AspNetPager1.PageSize, this.hfType.Value).Tables[0];
                    sbHtml.Append(ListHtml(dt));
                }
            }
            else if (isRelationShipPublicity)
            {
                dtCount = null;
                dtCount = PartyRelationnshipPublicity.GetListData(-1, -1).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyRelationnshipPublicity.GetListData(startIndex, AspNetPager1.PageSize).Tables[0];
                sbHtml.Append(ListHtml(dt));
            }
            else if (isPartyPayment)
            {
                string sss = "";
                if (this.BranchID == "")
                {

                    try
                    {
                        if (this.CurrentUser.BranchID == "")
                        {
                            sss = "";
                        }
                        else
                        {
                            sss = this.CurrentUser.BranchID;
                        }
                    }
                    catch (Exception ex)
                    {
                        sss = "";
                    }
                }
                else
                {
                    sss = this.BranchID;
                }

                dtCount = null;
                dtCount = PartyMemberCost.GetMemberCost(-1, -1, sss).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyMemberCost.GetMemberCost(startIndex, AspNetPager1.PageSize, sss).Tables[0];
                sbHtml.Append(ListHtml(dt));
            }
            else if (isDownloadCadreWork)
            {
                dtCount = null;
                dtCount = PartyArticle.GetMoreArticleTitle(-1, -1, this.BranchID, module, this.Category, isDownloadCadreWork).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyArticle.GetMoreArticleTitle(AspNetPager1.PageSize, startIndex, this.BranchID, module, this.Category, isDownloadCadreWork).Tables[0];
                sbHtml.Append(ListHtml(dt));
            }
            else if (isTreasureBox)
            {
                dtCount = null;
                dtCount = PartyArticle.GetMoreArticleTitle(-1, -1, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyArticle.GetMoreArticleTitle(AspNetPager1.PageSize, startIndex, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span><a href=\"" + Convert.ToString(item["Url"]) + "\" >下载</a></span>");
                    sbHtml.Append("<a target =\"_blank\"  href=\"" + Convert.ToString(item["Url"]) + "\" > " + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else if (isDownloadLibrary)
            {
                dtCount = null;
                dtCount = PartyArticle.GetMoreArticleTitle(-1, -1, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyArticle.GetMoreArticleTitle(AspNetPager1.PageSize, startIndex, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span><a target=\"_blank\" href=\"" + Convert.ToString(item["AuthorUrl"]) + "\" >下载</a></span>");
                    sbHtml.Append("<a target=\"_blank\" href=\"" + Convert.ToString(item["AuthorUrl"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else if (isDownloadCadreWork)
            {
                dtCount = null;
                dtCount = PartyArticle.GetMoreArticleTitle(-1, -1, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyArticle.GetMoreArticleTitle(AspNetPager1.PageSize, startIndex, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span><a href=\"" + Convert.ToString(item["Url"]) + "\" >下载</a></span>");
                    sbHtml.Append("<a target =\"_blank\"  href=\"" + Convert.ToString(item["Url"]) + "\" > " + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else if (isVolunteerActivitie)
            {
                dtCount = null;
                dtCount = PartyVolunteerActivitie.GetMoreActivitieTitle(-1, -1).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyVolunteerActivitie.GetMoreActivitieTitle(AspNetPager1.PageSize, startIndex).Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd") + "</span>");
                    sbHtml.Append("<a target =\"_blank\"  href=\"Portal/VolunteerActivitie/VolunteerActiviteDetail.aspx?ID=" + Convert.ToString(item["ID"]) + "\" >[" + Convert.ToString(item["IsEndName"]) + "]" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else if (isVolunteerQuotation)
            {
                dtCount = null;
                dtCount = PartyVolunteerQuotation.GetMoreQuotationTitle(-1, -1).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyVolunteerQuotation.GetMoreQuotationTitle(AspNetPager1.PageSize, startIndex).Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd") + "</span>");
                    sbHtml.Append("<a target =\"_blank\"  href=\"Portal/VolunteerActivitie/VolunteerQuotationDetail.aspx?ID=" + Convert.ToString(item["ID"]) + "\" >" + Convert.ToString(item["Content"]) + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else if (isJoinPartyPublicity)
            {
                dtCount = null;
                dtCount = PartyArticle.GetMoreArticleTitle(-1, -1, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyArticle.GetMoreArticleTitle(AspNetPager1.PageSize, startIndex, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd HH:mm:ss") + "</span>");
                    switch (item["StepID"].ToString())
                    {
                        case "JoinStep4": sbHtml.Append("<a target =\"_blank\"  href=\"Portal/JoinParty/Activists.aspx?ID=" + Convert.ToString(item["ID"]) + "&AppllicationID=" + Convert.ToString(item["AppllicationID"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>"); break;
                        case "JoinStep12": sbHtml.Append("<a target =\"_blank\"  href=\"Portal/JoinParty/PotentialMember.aspx?ID=" + Convert.ToString(item["ID"]) + "&AppllicationID=" + Convert.ToString(item["AppllicationID"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>"); break;
                        case "JoinStep24": sbHtml.Append("<a target =\"_blank\"  href=\"Portal/JoinParty/ProbationaryMember.aspx?ID=" + Convert.ToString(item["ID"]) + "&AppllicationID=" + Convert.ToString(item["AppllicationID"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>"); break;
                        case "JoinStep36": sbHtml.Append("<a target =\"_blank\"  href=\"Portal/JoinParty/Regular.aspx?ID=" + Convert.ToString(item["ID"]) + "&AppllicationID=" + Convert.ToString(item["AppllicationID"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>"); break;
                    }

                    sbHtml.Append("</li>");
                }
            }
            else if (isRelationShipPublicity)
            {
                dtCount = null;
                dtCount = PartyArticle.GetMoreArticleTitle(-1, -1, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyArticle.GetMoreArticleTitle(AspNetPager1.PageSize, startIndex, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd HH:mm:ss") + "</span>");
                    sbHtml.Append("<a target =\"_blank\"  href=\"#\" >" + CutLongTitle(Convert.ToString(item["Title"]), 52) + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else if (isPartyPayment)
            {
                dtCount = null;
                dtCount = PartyArticle.GetMoreArticleTitle(-1, -1, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyArticle.GetMoreArticleTitle(AspNetPager1.PageSize, startIndex, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd HH:mm:ss") + "</span>");
                    sbHtml.Append("<a \" >" + Convert.ToString(item["UserName"]) + "缴纳" + Convert.ToString(item["Year"]) + "年" + Convert.ToString(item["Month"]) + "月党费" + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else if (isMassLineSuperiorsSpirit)
            {
                dtCount = null;
                dtCount = PartyArticle.GetMassLineSuperiorSpirit(-1, -1, "", module, this.Category).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyArticle.GetMassLineSuperiorSpirit(startIndex, AspNetPager1.PageSize, "", module, this.Category).Tables[0];
                foreach (DataRow item in dt.Rows)
                {
                    sbHtml.Append("<li>");
                    sbHtml.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd HH:mm:ss") + "</span>");
                    sbHtml.Append("<a target =\"_blank\"  href=\"ArticleDetailContent.aspx?ID=" + Convert.ToString(item["ID"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>");
                    sbHtml.Append("</li>");
                }
            }
            else
            {
                dtCount = null;
                dtCount = PartyArticle.GetMoreArticleTitle(-1, -1, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                BindPageControl(dtCount, sumCount);

                dt = PartyArticle.GetMoreArticleTitle(AspNetPager1.PageSize, startIndex, this.BranchID, module, this.Category, isTreasureBox).Tables[0];
                sbHtml.Append(ListHtml(dt));
            }

            liList.Text = sbHtml.ToString();
        }

        /// <summary>
        /// 绑定分页控件
        /// </summary>
        private void BindPageControl(DataTable dtCount, int sumCount)
        {
            if (dtCount != null)
                sumCount = dtCount.Rows.Count;
            AspNetPager1.RecordCount = sumCount;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            LoadData();
            hfType.Value = this.Request["type"] ?? "";
            hfSearchText.Value = SearchText;
            BindData();
        }

        /// <summary>
        /// 布局拼接字符串
        /// </summary>
        /// <param name="dtHtml"></param>
        /// <returns></returns>
        private string ListHtml(DataTable dtHtml)
        {
            StringBuilder sb = new StringBuilder();
            foreach (DataRow item in dtHtml.Rows)
            {
                sb.Append("<li>");
                sb.Append("<span>" + Convert.ToDateTime(item["CreateTime"]).ToString("yyyy-MM-dd HH:mm:ss") + "</span>");
                sb.Append("<a target =\"_blank\"  href=\"ArticleDetailContent.aspx?ID=" + Convert.ToString(item["ID"]) + "\" >" + CutLongTitle(Convert.ToString(item["Title"]), 42) + "</a>");
                sb.Append("</li>");
            }
            return sb.ToString();
        }

        /// <summary>
        /// 截取标题
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public string CutLongTitle(object obj, int length)
        {
            string title = Convert.ToString(obj);
            if (title.Length > length)
            {
                title = title.Substring(0, length) + "...";
            }
            return title;
        }
    }
}